package Queuedemo;

class MyCircularQueue {
    public int[] elem;
    public int front;//队头
    public int rear;//队尾

    //创建循环队列
    public MyCircularQueue(int k) {
        elem = new int[k];
    }
    //插入元素
    public boolean enQueue(int value) {
        //判断队列是否已满
        if(isFull()) {
            return false;
        }
        elem[rear] = value;
        rear = (rear + 1) % elem.length;
        return true;
    }

    //删除元素
    public boolean deQueue() {
        //判断队列是否为空
        if(isEmpty()) {
            return false;
        }
        front = (front + 1) % elem.length;
        return true;
    }

    //获取队首元素
    public int Front() {
        if(isEmpty()) {
            return -1;
        }
        return elem[front];
    }

    //获取队尾元素
    public int Rear() {
        if(isEmpty()) {
            return -1;
        }
        return elem[(rear + elem.length - 1) % elem.length];
    }

    //判断队列是否为空
    public boolean isEmpty() {
        return rear == front;
    }

    //判断队列是否已满
    public boolean isFull() {
        return (rear + 1) % elem.length == front;
    }
}